package com.ruoyi.quartz.aop;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.service.IMonitorLogService;
import com.ruoyi.business.common.AsyncService;
import com.ruoyi.business.util.BathJobMonitorUtil;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
@Order(2)
public class BathJobAspect {

    @Autowired
    RedisCache redisCache;

    @Autowired
    ISysConfigService iSysConfigService;

    @Autowired
    IMonitorLogService iMonitorLogService;

    @Autowired
    BathJobMonitorUtil bathJobMonitorUtil;

    @Autowired
    AsyncService asyncService;

//    @Autowired
//    IFeishuService iFeishuService;

    @Pointcut("execution(* com.ruoyi.quartz.task.BatchJob.*(..))" +
            " || execution(* com.ruoyi.quartz.task.CollectionTask.*(..))"+
            "")
    public void Pointcut() {
    }

    @Around("Pointcut()")
    //环绕通知需要得知方法的参数    (一个接口方法的参数)
    public Object aroundSysLog(ProceedingJoinPoint pjp){

        log.info("进来了BathJobAspect");

        //获取当前执行方法所需的参数
        String name = pjp.getSignature().getDeclaringType().getName();
        String[] split = name.split("\\.");
        String apiName = split[split.length-1];

        //清理缓存
        bathJobMonitorUtil.clearRedis(apiName);

        try {
            Object result = pjp.proceed();
            //删除缓存
            bathJobMonitorUtil.delRedis(apiName);
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    @AfterThrowing(pointcut = "Pointcut()", throwing = "ex")
    public void doAfterThrowing(JoinPoint joinPoint, Exception ex) {

        log.error("批量任务监控报错:",ex);

        String name = joinPoint.getSignature().getDeclaringType().getName();
        String[] split = name.split("\\.");
        String apiName = split[split.length-1];

        //报错直接发消息
//        String content = "";
//        content += "[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"*BatchJobError*\"}},";
//        content += "{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"";
//        content += "*`Time：`* " + DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss") + ";\\n";
//        content += "*`ClassName：`* " + apiName + ";\\n";
//        content += "*`Error：`* " + ex.getMessage() + ";\\n";
//        content += "\"}}]";

        JSONObject content = new JSONObject();
        content.put("Time",DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        content.put("ClassName",apiName);
        content.put("Error",ex.getMessage());

        //iFeishuService.sendMessageByName("monitor-group",content,"BatchJobError");
        asyncService.sendFeishu("monitor-group","BatchJobError",content);
    }

}
